<template>
  <div class="app-container">
    <el-button type="primary" plain @click="add">新增</el-button>
    <div style="padding: 10px" />

    <el-table
      v-loading="listLoading"
      :data="tableData"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      default-expand-all
      row-key="djDeptId"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column label="部门名称" prop="djDeptName" />
      <el-table-column label="排序" align="center" prop="orderNum" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            plain
            @click="edit(scope.row.djDeptId)"
          >
            编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            plain
            v-if="scope.row.parentId != 0"
            @click="remove(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      width="350px"
      :title="title"
      :visible.sync="visible"
      :close-on-click-modal="false"
      :show-close="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="6em">
        <el-form-item
          label="上级部门"
          prop="parentId"
          v-if="form.parentId !== 0"
        >
          <tree-select
            v-model="form.parentId"
            :options="tableData"
            :treeProps="treeProps"
            placeholder="选择上级部门"
          />
        </el-form-item>
        <el-form-item label="机构名称" prop="djDeptName">
          <el-input v-model="form.djDeptName" placeholder="请输入机构名称" />
        </el-form-item>
        <el-form-item label="显示排序" prop="orderNum">
          <el-input-number
            style="width: 100%"
            v-model="form.orderNum"
            controls-position="right"
            :min="0"
          />
        </el-form-item>
        <el-form-item style="margin-bottom: 0">
          <el-button @click="visible = false">关 闭</el-button>
          <el-button type="primary" @click="submit">提 交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  getZuzhiList,
  getZuzhiDetail,
  addZuzhi,
  editZuzhi,
  deleteZuzhi,
} from "@/api2/partyManage";
import { handleTree } from "@/utils/utils";
import moment from "moment";
import treeSelect from "./components/treeSelect";

export default {
  components: { treeSelect },
  data() {
    return {
      tableData: [],
      listLoading: true,
      visible: false,
      title: "",
      treeProps: {
        value: "djDeptId", // ID字段名
        label: "djDeptName", // 显示名称
        children: "children", // 子级字段名
      },
      form: {},
      rules: {
        parentId: [
          {
            required: true,
            message: "上级部门不能为空",
            trigger: ["blur", "change"],
          },
        ],
        djDeptName: [
          { required: true, message: "部门名称不能为空", trigger: "blur" },
        ],
        orderNum: [
          { required: true, message: "显示排序不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 获取格式化时间字符串
    getFormatedTimeStr(stamp, str = "MM-DD HH:mm") {
      return moment(stamp).format(str);
    },
    async getList() {
      this.listLoading = true;
      await this.getTreeData();
      this.listLoading = false;
    },
    async getTreeData() {
      const res = await getZuzhiList();
      this.tableData = handleTree(res.data, "djDeptId");
    },
    add() {
      this.title = "新增";
      this.visible = true;
      this.resetForm();
      this.form = {};
    },
    async edit(id) {
      this.title = "编辑";
      this.visible = true;
      await this.getTreeData();
      const res = await getZuzhiDetail(id);
      this.form = res.data;
    },
    submit() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          if (this.form.djDeptId) {
            const { djDeptId, parentId, orderNum, djDeptName } = this.form;
            const params = { djDeptId, parentId, orderNum, djDeptName };
            await editZuzhi(params);
            this.$message.success("修改成功");
          } else {
            await addZuzhi(this.form);
            this.$message.success("新增成功");
          }
          this.visible = false;
          this.getList();
        }
      });
    },
    resetForm() {
      if (!this.$refs.form) return;
      this.$refs.form.resetFields();
    },
    remove(row) {
      this.$confirm("是否删除名称为 " + row.djDeptName + " 的数据项?", {
        type: "warning",
      })
        .then(async () => {
          await deleteZuzhi(row.djDeptId);
          this.$message.success("删除成功");
          this.getList();
        })
        .catch(() => {});
    },
  },
};
</script>
